package com.daisuxia.back.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.daisuxia.web.pojo.BorrowOrder;
import com.daisuxia.web.util.GenerateNo;
import com.daisuxia.web.util.JSONUtil;
import com.daisuxia.web.util.encrypt.MD5coding;

import net.sf.json.JSONObject;

public class Assetdockingkd {

	/**
	 * @param args
	 */
	public static void addDocking(BorrowOrder bo) {
		bo = new BorrowOrder();
		bo.setRealname("张三");
		bo.setUserPhone("17714511144");
		bo.setLoanInterests(250);
		bo.setLoanMethod(0);
		bo.setLoanTerm(35);
		bo.setCardNo("464676746443131313");
		bo.setMoneyAmount(36555);
		bo.setIdNumber("422802199210144555");
		String userAccount = "kdlc_xjx";
		String userPwd = "123456";
		String id_number = bo.getIdNumber();
		String timestamp = String.valueOf(new Date().getTime());
		String card_no = bo.getCardNo();
		String name = bo.getRealname();// bo.getRealname()
		String phone = bo.getUserPhone();
		Integer counter_fee = bo.getLoanInterests();// 手续费,单位：分
		Integer loan_method = bo.getLoanMethod();// 贷款方式(0:按天,1:按月,2:按年)
		Integer loan_term = bo.getLoanTerm();// 期限
		String apr = "0";// //贷款年化率利率
		Integer money_amount = bo.getMoneyAmount();// 贷款金额，单位：分
		String loan_interests = "0";// 总利息
		Integer repayment_amount = bo.getMoneyAmount();// 总还款金额,单位：分
		String repayment_principal = "233";// 待还本金
		String repayment_interest = "0";// 利息
		String bank_id = "12";

		String repayment_time = "201511001111";// 还款时间
		String order_time = "201511001111";
		String orderNo = GenerateNo.nextOrdId();

		Map<String, Object> objj = new HashMap<String, Object>();
		String sign = MD5coding.MD5(userAccount + MD5coding.MD5(userPwd) + MD5coding.MD5(id_number) + timestamp);
		objj.put("sign", sign);// /?????????????????????????md5(account+md5(pwd)+md5(id_number)+timestamp)
		objj.put("account", userAccount);
		objj.put("id_number", id_number);
		objj.put("timestamp", timestamp);
		// user_base用户必填信息
		Map<String, Object> user_base = new HashMap<String, Object>();
		user_base.put("id_number", id_number);
		user_base.put("name", name);
		user_base.put("phone", phone);
		// user_base.put("education_level", "3");// 学历非必填
		user_base.put("property", "男");
		user_base.put("type", "2");// 借款人类型(1:企业，2:个人)
		objj.put("user_base", user_base);
		// receive_card打款银行卡
		Map<String, Object> receive_card = new HashMap<String, Object>();
		receive_card.put("bank_id", bank_id);// 银行卡id
		receive_card.put("card_no", card_no);
		receive_card.put("phone", phone);
		receive_card.put("name", name);// 学历非必填
		objj.put("receive_card", receive_card);

		// debit_card扣款银行卡，不代扣可不传
		Map<String, Object> debit_card = new HashMap<String, Object>();
		debit_card.put("bank_id", bank_id);// 银行卡id
		debit_card.put("card_no", card_no);
		debit_card.put("phone", phone);
		objj.put("debit_card", debit_card);

		// order_base借款订单信息
		Map<String, Object> order_base = new HashMap<String, Object>();
		order_base.put("out_trade_no", orderNo);// 各厂商自己平台申请单号
		order_base.put("money_amount", money_amount);// 贷款金额，单位：分
		order_base.put("loan_method", loan_method);
		order_base.put("loan_term", loan_term);// 贷款期限,
		order_base.put("loan_interests", loan_interests);// 总共利息,单位：分
		order_base.put("apr", apr);// 贷款年化率利率
		order_base.put("order_time", order_time);// 下单时间时间戳表示
		order_base.put("counter_fee", counter_fee);// 手续费,单位：分

		objj.put("order_base", order_base);

		// repayment_base总还款信息
		Map<String, Object> repayment_base = new HashMap<String, Object>();
		repayment_base.put("repayment_type", 2);// 还款方式，0:等本等息,1:等额本息,2:一次性还款
		repayment_base.put("repayment_amount", repayment_amount);// 总还款金额,单位：分
		repayment_base.put("repayment_time", repayment_time);// 最迟还款日期时间戳
		repayment_base.put("period", 1);// 总还款期数
		repayment_base.put("repayment_principal", repayment_principal);// 还款本金,单位：分
		repayment_base.put("repayment_interest", repayment_interest);// 还款利息,单位：分
		objj.put("repayment_base", repayment_base);

		Map[] period_base = new Map[1];
		// period_base还款计划信息
		Map<String, Object> period_base1 = new HashMap<String, Object>();

		period_base1.put("plan_repayment_money", repayment_amount);// 预期还款金额,单位：分
		period_base1.put("plan_repayment_time", repayment_time);// 预期还款时间戳
		period_base1.put("period", 1);// 总还款期数
		period_base1.put("plan_repayment_principal", repayment_principal);// 还款本金,单位：分
		period_base1.put("plan_repayment_interest", repayment_interest);// 还款利息,单位：分\
		period_base1.put("apr", apr);// 还款利息,单位：分\
		period_base[0] = period_base1;
		objj.put("period_base", period_base);

		String url = "http://42.96.204.114/koudai/kdkj/frontend/web/interface/create-order";

		String paramsJosn = JSONUtil.beanToJson(objj);
		System.out.println(paramsJosn);

		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("soketOut", 1000);
		params.put("connOut", 1000);
		try {

			String result = new WebClient().postJsonData(url, paramsJosn, params);
			if(result==null){
				
				System.out.println("连接超时");
				return;
			}
			System.out.println(result);

			JSONObject jsonObj = JSONObject.fromObject(result);

			String code = jsonObj.getString("code");
			if (code.equals("0")) {

				String outNo = jsonObj.getString("order_id");
				System.out.println(outNo);
			} else {
				String message = jsonObj.getString("message");
				System.out.println(code + ":" + message);
			}
		} catch (Exception e) {
		if(e.getMessage().contains("connect timed out")){
			
			System.out.println("请求超时");
		}
		}

	}

	public static void main(String args[]) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date newDate=new Date();
	try {
//		System.out.println(String.valueOf(sdf.parse(sdf.format(newDate)).getTime()/1000));
		System.out.println(String.valueOf(newDate.getTime()/1000));
		
		System.out.println(sdf.format(new Date()));
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
//		addDocking(null);
	}
}
